package oop.day05;

public class RefDemo02 {
    public static void main(String[] args) {
        // 向上造型 小类的对象 归结为 大类的类型
      Animal dog =  new Dog("小白",3,"白色");// 向上造型
      Animal fish =  new Fish("小金",2,"金色");
      Chick chick = new Chick("小蓝",7,"蓝色");// 创建对象的时候  本身的类型

      Animal[] animals = new Animal[3];// 定义了animal类型的数组，
      animals[0] = dog;
      animals[1] = fish;
      animals[2] = chick;// 放的时候 进行了向上 造型
      for (Animal a : animals) {
          a.drink();
          a.eat();
          if (a instanceof Dog) {
              Dog d = (Dog) a;
              d.lookHome();
              d.swim();
          }
          if (a instanceof Fish) {
              Fish f = (Fish) a;
              f.swim();
          }
          if (a instanceof Chick) {
              Chick c = (Chick) a;
              c.leyegg();
          }
       /*  Dog d = (Dog) a;
         d.lookHome();*/

      }
        System.out.println("------------------");
      Dog dog1= new Dog();// 本身类型  狗是狗
        dog1.eat();// animal 继承的抽象重写方法
        dog1.drink();// animal 继承的方法
        dog1.lookHome();// 自身特有的方法
        dog1.swim();//接口中继承实现的方法
        System.out.println(dog1 instanceof Dog);// 判断dog1 是不是Dog 类型
      Animal dog2= new Dog();// 向上造型  狗是动物
        dog2.eat();
        dog2.drink();
       // dog2.lookHome();// 不能调用
       // dog2.swim();//不能调用
        System.out.println(dog2 instanceof Dog);
      Dog dog4= (Dog) dog2;// 强转
      dog4.lookHome();
      Swim dog3= new Dog(); // 向上造型， 狗会游泳
       // dog3.eat();
       // dog3.drink();
      //  dog3.lookHome();
        dog3.swim();
        System.out.println(dog3 instanceof Dog);
      Dog dog5= (Dog) dog3;
      dog5.lookHome();
    }
}
